{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from azure.storage.file import FileService\n",
    "import json\n",
    "import os\n",
    "from PIL import Image, ImageDraw\n",
    "import pandas as pd\n",
    "import re\n",
    "import numpy as np\n",
    "from eMammal_helpers import draw_bboxes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# print all outputs in a cell\n",
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = \"all\"\n",
    "\n",
    "# auto reload external Python modules\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Check the annotations from iMerit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Download the annotation file from File Share to temp folder on local - **Do once**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "key = os.environ[\"AZ_STORAGE_KEY\"]\n",
    "file_service = FileService(account_name='ai4edevshare', account_key=key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "annotation_path = '/home/yasiyu/yasiyu_temp/eMammal_annotations/microsoft_batch6_29Sept2018.json'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_service.get_file_to_path('ai4edevfs', 'annotations/incoming_annotations', os.path.basename(annotation_path), annotation_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read in the annotations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(annotation_path, 'r') as f:\n",
    "    content = f.readlines()\n",
    "\n",
    "len(content)  # from the filename, all appears to be of the eMammal dataset. Length is 1848"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "json.loads(content[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "emammal = []\n",
    "others = []\n",
    "emammal_num_images = 0\n",
    "emammal_images_with_groups = []\n",
    "\n",
    "for row in content:\n",
    "    entry = json.loads(row)\n",
    "    assert len(entry['images']) > 0\n",
    "    \n",
    "    file_name = entry['images'][0]['file_name']\n",
    "    if file_name.startswith('datasetemammal.'):\n",
    "        emammal.append(file_name)\n",
    "        emammal_num_images += len(entry['images'])\n",
    "        \n",
    "        for bbox in entry['annotations']:\n",
    "            if bbox['category_id'] == 3:\n",
    "                emammal_images_with_groups.add(bbox['image_id'])\n",
    "    else:\n",
    "        others.append(file_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(emammal)  # each entry is one sequence, so length should be the number of sequences\n",
    "emammal_num_images\n",
    "len(others)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "emammal_images_with_groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "diff_anno_img_count = 0\n",
    "for row in content:\n",
    "    entry = json.loads(row)\n",
    "    if len(entry['annotations']) != len(entry['images']):\n",
    "        diff_anno_img_count += 1\n",
    "    if len(entry['categories']) != 3:  # since Sept 29 batch, new category 'group' added\n",
    "        print('categories are {}'.format(entry['categories']))\n",
    "diff_anno_img_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# put the annotations in a dataframes so we can select all annotations for a given image\n",
    "annotations = []\n",
    "images = []\n",
    "for row in content:\n",
    "    entry = json.loads(row)\n",
    "    annotations.extend(entry['annotations'])\n",
    "    images.extend(entry['images'])\n",
    "    \n",
    "df_anno = pd.DataFrame(annotations)\n",
    "df_img = pd.DataFrame(images)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_anno.sample(n=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_anno.iloc[0].image_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_img.sample(n=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(df_anno)\n",
    "len(df_img)  # some images had no annotations aka empty"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Visualize the bboxes on a few images to spot check.\n",
    "\n",
    "Visualize 100 random images from the returned batch. Not all images have annotations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "viz_output_dir = '/home/yasiyu/yasiyu_temp/viz_output'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample_img = df_img.sample(n=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "labeled_images = []\n",
    "no_annotation_images = []\n",
    "\n",
    "# the dash between seq and frame is different among the batches\n",
    "pattern = re.compile('^datasetemammal\\.project(.+?)\\.deployment(.+?)\\.seq(.+?)[-_]frame(.+?)\\.img(.+?)\\.')\n",
    "\n",
    "for i in range(len(sample_img)):\n",
    "    file_name = sample_img.iloc[i]['file_name']\n",
    "    match = pattern.match(file_name)\n",
    "    project_id, deployment_id, seq_id, frame_order, image_id = match.group(1, 2, 3, 4, 5)\n",
    "    img_path1 = '/datadrive/emammal/{}{}/{}.jpg'.format(project_id, deployment_id, image_id)\n",
    "    img_path2 = '/datadrive/emammal/{}{}/{}.JPG'.format(project_id, deployment_id, image_id)\n",
    "    img_path = img_path1 if os.path.exists(img_path1) else img_path2\n",
    "\n",
    "    annos_i = df_anno.loc[df_anno['image_id'] == file_name, :]  # all annotations on this image\n",
    "    \n",
    "    if len(annos_i) > 0:\n",
    "        bboxes = list(annos_i.loc[:, 'bbox'])\n",
    "        classes = list(annos_i.loc[:, 'category_id'])\n",
    "         \n",
    "        if not os.path.exists(img_path):\n",
    "            print('Image {} cannot be found at the path.'.format(img_path))\n",
    "            continue\n",
    "        labeled_img = draw_bboxes(img_path, bboxes, classes, show_label=True)\n",
    "        #labeled_img.save(os.path.join(viz_output_dir, '{}_gtbbox.png'.format(file_name.lower().split('.jpg')[0])))\n",
    "        labeled_images.append(labeled_img)\n",
    "    else:\n",
    "        print('No annotations found for image {}.'.format(file_name))\n",
    "        if not os.path.exists(img_path):\n",
    "            print('! Image also cannot be found.')\n",
    "            continue\n",
    "        no_annotation_images.append(Image.open(img_path))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "labeled_images[10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "labeled_images[11]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "no_annotation_images[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "no_annotation_images[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Visualize some images with persons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "imgs_with_persons = df_anno[df_anno.human_visible == 1].image_id\n",
    "len(imgs_with_persons)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:py35]",
   "language": "python",
   "name": "conda-env-py35-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
